<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission('0',1)">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" align="center" label="标识" />
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <el-button v-if="row.type === 1" type="text" @click="addPermission(row.id,2)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增、修改 -->
      <el-dialog
        title="弹框"
        :visible.sync="showDialog"
        width="50%"
        @close="btnCancel"
      >
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="showDialog = false">Cancel</el-button>
          <el-button type="primary" @click="btnOk">OK</el-button>
        </span>
      </el-dialog>
    </div>

  </div>
</template>
<script>
import { updatePermission, addPermission, getPermissionDetail, delPermission, getPermissionList } from '@/api/permissions'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {},
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async  getPermissionList() {
      const res = await getPermissionList()
      this.list = tranListToTreeData(res, '0')
    },
    // 增加
    addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // 修改
    async editPermission(id) {
      // 获取这行的数据，然后回显
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    // 删除
    async delPermission(row) {
      try {
        await this.$confirm('确定要删除该数据吗')
        await delPermission(row.id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 提交行为
    async btnOk() {
      try {
        // 校验
        await this.$refs.perForm.validate()
        // 发送请求
        if (this.formData.id) {
          // 修改
          await updatePermission(this.formData)
        } else {
          // 增加
          await addPermission(this.formData)
        }
        this.$message.success('操作成功')
        this.showDialog = false
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭
    btnCancel() {
      this.formData = {}
      this.$refs.perForm.resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
